package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.module.admin.entity.SysFood;
import com.mars.module.admin.mapper.SysFoodMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.SysOrderDetailsRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.SysOrderDetailsMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.SysOrderDetails;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.ISysOrderDetailsService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单明细业务层处理
 *
 * @author mars
 * @date 2024-04-15
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysOrderDetailsServiceImpl implements ISysOrderDetailsService {

    private final SysOrderDetailsMapper baseMapper;
    private final SysFoodMapper foodMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysOrderDetails add(SysOrderDetailsRequest request) {
        SysOrderDetails entity = SysOrderDetails.builder().build();
        BeanUtils.copyProperties(request, entity);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysOrderDetailsRequest request) {
        SysOrderDetails entity = SysOrderDetails.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public SysOrderDetails getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<SysOrderDetails> pageList(SysOrderDetailsRequest request) {
        Page<SysOrderDetails> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<SysOrderDetails> query = this.buildWrapper(request);
        IPage<SysOrderDetails> pageRecord = baseMapper.selectPage(page, query);
        List<SysOrderDetails> records = pageRecord.getRecords();
        List<SysOrderDetails> recordsNew = new ArrayList<>();
        for (SysOrderDetails details : records) {
            SysFood food = foodMapper.selectById(details.getProductId());
            details.setProductName(food == null ? "" : food.getName());
            recordsNew.add(details);
        }
        pageRecord.setRecords(recordsNew);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<SysOrderDetails> list(SysOrderDetailsRequest request) {
        LambdaQueryWrapper<SysOrderDetails> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<SysOrderDetails> buildWrapper(SysOrderDetailsRequest param) {
        LambdaQueryWrapper<SysOrderDetails> query = new LambdaQueryWrapper<>();
        if (param.getAmount() != null) {
            query.eq(SysOrderDetails::getAmount, param.getAmount());
        }
        if (param.getOrderId() != null) {
            query.eq(SysOrderDetails::getOrderId, param.getOrderId());
        }
        return query;
    }

}
